package matse11.lspiel;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;

public class SpielView extends View {

	// variablen dec
	final int dim = 480;// px
	final int aFieldDim = 480 / 4;
	final int toTop = 32;

	final int inTop = 520;
	final int inDim = 204;
	final int inAField = inDim / 3;
	final int inRight = 138;

	private int myX = 0;
	private int myY = 0;
	
	

	private OnTouchListener touch = new OnTouchListener() {
		@Override
		public boolean onTouch(View arg0, MotionEvent arg1) {
			// TODO Auto-generated method stub
			myX = (int) arg1.getX();
			myY = (int) arg1.getY();
			if (myY <= dim + toTop && myY >= toTop) {
				myY -= toTop;
				myX = myY / aFieldDim;
				myY = (int) arg1.getX() / aFieldDim;
			}

			if (myY <= inTop + inDim && myY >= inTop && myX >= inRight
					&& myX <= inRight + inDim) {
				myX = (int) ((arg1.getY() - inTop) / inAField);
				myY = (int) ((arg1.getX() - inRight) / inAField);
				
				if(myX == 0){
					if(myY == 0){
						System.out.println("Drehe Links");
					}else if(myY == 1){
						System.out.println("Nach oben");
					}else{
						System.out.println("Drehe Rechts");
					}
				}else if(myX == 1){
					if(myY == 0){
						System.out.println("Nach Links");
					}else if(myY == 1){
						System.out.println("\"OK\"");
					}else{
						System.out.println("Nach Rechts");
					}
				}else{
					if(myY == 0){
						System.out.println("Spiegel Horizontal");
					}else if(myY == 1){
						System.out.println("Nach Unten");
					}else{
						System.out.println("Spiegel Vertikal");
					}
				}
			}
			invalidate(new Rect(0, 0, dim, toTop + dim));
			return false;
		}
	};

	public SpielView(Context context) {
		super(context);
		this.setOnTouchListener(touch);
		// this.draw(myCanvas);
	}

	@Override
	protected void onDraw(Canvas c) {
		super.onDraw(c);

		System.out.println("onDraw!!!!");

		Paint paint = new Paint();

		// method connntent
		paint.setStyle(Paint.Style.FILL);

		// zeichne den geasammtenhintergrund schwarz
		paint.setColor(Color.BLACK);
		c.drawPaint(paint);

		// zeichne das spielfeld bg:
		paint.setColor(Color.WHITE);
		c.drawRect(0, toTop, dim, toTop + dim, paint);
		// linien
		paint.setColor(Color.DKGRAY);
		for (int i = 0; i < 4; i++) {
			c.drawLine(aFieldDim * i, toTop, aFieldDim * i, toTop + dim, paint);
			c.drawLine(0, toTop + aFieldDim * i, dim, toTop + aFieldDim * i,
					paint);
		}

		paint.setColor(Color.YELLOW);
		c.drawText("Pos: " + myX + " X " + myY, 0, toTop, paint);

		paint.setColor(Color.BLACK);
		c.drawRect(inRight, inTop, inRight + inDim, inTop + inDim, paint);

		// pfeile zeichnen
		Resources res = this.getContext().getResources();
		Drawable image = res.getDrawable(R.drawable.links);
		image.setBounds(inRight + 1, inTop + inAField + 1, inRight + inAField
				- 1, inTop + inAField * 2 - 1);
		image.draw(c);

		// oben
		image = res.getDrawable(R.drawable.oben);
		image.setBounds(inRight + inAField + 1, inTop + 1, inRight + inAField
				* 2 - 1, inTop + inAField - 1);
		image.draw(c);

		// rechts
		image = res.getDrawable(R.drawable.rechts);
		image.setBounds(inRight + inAField * 2 + 1, inTop + inAField + 1,
				inRight + inAField * 3 - 1, inTop + inAField * 2 - 1);
		image.draw(c);

		// unten
		image = res.getDrawable(R.drawable.unten);
		image.setBounds(inRight + inAField + 1, inTop + inAField * 2 + 1,
				inRight + inAField * 2 - 1, inTop + inAField * 3 - 1);
		image.draw(c);

		// links drehen
		image = res.getDrawable(R.drawable.linksdrehen);
		image.setBounds(inRight+1, inTop+ 1, inRight + inAField - 1, inTop + inAField  - 1);
		image.draw(c);

		// rechts drehen
		image = res.getDrawable(R.drawable.rechtsdrehen);
		image.setBounds(inRight+inAField*2+1, inTop+ 1, inRight + inAField*3 - 1, inTop + inAField  - 1);
		image.draw(c);
		
		// spiegel 1
		image = res.getDrawable(R.drawable.spiegelho);
		image.setBounds(inRight+1, inTop+inAField*2+ 1, inRight + inAField - 1, inTop + inAField*3  - 1);
		image.draw(c);
		
		// spiegel 1
		image = res.getDrawable(R.drawable.spiegelve);
		image.setBounds(inRight+inAField*2+1, inTop+inAField*2+ 1, inRight + inAField*3 - 1, inTop + inAField*3  - 1);
		image.draw(c);
		
		// mitte
		paint.setColor(Color.GREEN);
		c.drawRect(inRight+inAField+2, inTop+inAField+2, inRight+inAField*2-2, inTop+inAField*2-2, paint);
		paint.setColor(Color.BLACK);
		paint.setTextSize(20);
		c.drawText("OK!", inRight+inAField*1.25f+2, inTop+inAField*1.6f+2, paint);
		
		
		// zeichne linien
		paint.setColor(Color.BLACK);
		for (int i = 0; i < 3; i++) {
			c.drawLine(inRight + inAField * i, inTop, inRight + inAField * i,
					inTop + inDim, paint);
			c.drawLine(inRight, inTop + inAField * i, inRight + inDim, inTop
					+ inAField * i, paint);
		}
	}

}
